<!DOCTYPE html>
<html lang="en">

<head>
    <title>品学联萌</title>
    <#include "/pxlm/app/product/components/meta.html" />
    <#include "/pxlm/app/product/components/stylesheets.html" />
    <link rel="stylesheet" href="/pxlm/app/product/stylesheets/majorInfo.css?v=1">
</head>

<body>
    <div id="app">
        <!-- <div class="title">{{ info.title ? info.title : '' }}</div>
        <div class="subtitle">{{ info.subtitle ? info.subtitle : '' }}</div>
        <div class="content">{{ info.cotnent ? info.cotnent : '' }}</div> -->
        <header data-am-widget="header" class="am-header am-header-default am-header-fixed" style="background:url('/pxlm/img/pxlmappbanner.png') no-repeat center">
            <h1 class="am-header-title">课程信息</h1>
        </header>
        <article data-am-widget="paragraph" class="am-paragraph am-paragraph-default" data-am-paragraph="{ tableScrollable: true, pureview: true }">
            <img :src='info.image' v-if="info.image" />
            <div class="title">{{info.title}}</div>
            <!-- <div class="am-text-sm">
                <span v-if='info.type==1'>日常班</span>
                <span v-if='info.type==2'>拓展班</span>
                <span v-if='info.type==3'>其他</span>
            </div> -->
            <p class='paragraph-default-p am-text-sm'>{{info.cotnent}}</p>
            <p class="paragraph-default-p" v-html="info.content"></p>
            <hr />
            <!-- <b class="am-text-md"><span class="am-icon-rocket"> </span> 课程价格</b>
                <div class="paragraph-default-p">¥: {{info.price}}</div>
            <hr /> -->
            <b class="am-text-md"><span class="am-icon-rocket"> </span> 备注</b>
            <p class="paragraph-default-p">{{info.remark}}</p>
            <!-- <hr />
            <a type="button" class="am-btn am-btn-success am-round am-btn-block" style="color: #fff;" href="'/pxlm/app/course/enroll?courseId=' + {{info.id}}">立即报名</a> -->
        </article>
        <div class="buttons">
            <div class="pay-btn" @click="goPay">立即报名</div>
            <div class="cart-btn" @click="addToCart">加入购物车</div>
        </div>
        <div class='popup' v-if="popupVisible">
            <div class='mask' @click="hideFormat"></div>
            <div class='popup-box'>
                <div class='popup-title'>选择规格<span class='popup-price' v-if="selectedPrice">￥{{ selectedPrice }}</span></div>
                <div class='popup-content'>
                    <div class='popup-item' v-bind:class="{ 'popup-selected' : item.id == formatId }" v-for="(item, index) in info.formats" :key="index" @click="onTapFormat(item)">{{ item.title }}</div>
                </div>
                <div class='confirm-container'>
                    <div class="confirm-btn" @click="goPay" v-if="btnType == 0">确定</div>
                    <div class="confirm-btn" @click="addToCart" v-else-if="btnType == 1">确定</div>
                </div>
            </div>
        </div>
    </div>
    <#include "/pxlm/app/product/components/scripts.html" />
    <script type="text/javascript">
    var app = new Vue({
        el: '#app',
        data() {
            return {
                info: {},
                productId: '${productId!}',
                formatId: '',
                selectedPrice: '',
                selectedformat: '',
                popupVisible: false,
                btnType: 0 // 0立即报名 1加入购物车
            }
        },
        mounted: function() {
            this.init();
        },
        methods: {
            init() {
                var url = '${host!}/pxlm/app/product/api/info';
                var params = {
                    productId: this.productId
                };
                var success = res => {
                    console.log(res);
                    this.info = res.info;
                }
                var fail = res => {
                    console.log(res);
                }
                var complete = () => {}
                request(url, params, success, fail, complete);
            },
            goPay() {
                if (this.info.formats && this.info.formats.length > 0 && !this.popupVisible) {
                    this.popupVisible = true;
                    this.btnType = 0;
                    return;
                }
                if (!this.formatId) {
                    msg("请选择规格");
                    return;
                }

                var url = '${host!}/pxlm/app/product/api/updateCart';
                var params = {
                    productId: this.productId,
                    formatId: this.formatId,
                    count: 1
                };
                var success = res => {
                    console.log(res);
                    var cart = new Array;
                    cart.push({
                        count: 1,
                        formatId: this.formatId,
                        formatTitle: this.selectedformat,
                        price: this.selectedPrice,
                        productId: this.productId,
                        title: this.info.title
                    });
                    sessionStorage.setItem("cart", JSON.stringify(cart));
                    window.location.href = '/pxlm/app/product/enroll';
                }
                var fail = res => {
                    console.log(res);
                }
                var complete = () => {}
                request(url, params, success, fail, complete);
            },
            addToCart() {
                if (this.info.formats && this.info.formats.length > 0 && !this.popupVisible) {
                    this.popupVisible = true;
                    this.btnType = 1;
                    return;
                }
                if (!this.formatId) {
                    msg("请选择规格");
                    return;
                }

                var url = '${host!}/pxlm/app/product/api/updateCart';
                var params = {
                    productId: this.productId,
                    formatId: this.formatId,
                    count: 1
                };
                var success = res => {
                    console.log(res);
                    msg("加入购物车成功");
                    this.popupVisible = false;
                }
                var fail = res => {
                    console.log(res);
                }
                var complete = () => {}
                request(url, params, success, fail, complete);
            },
            onTapFormat(item) {
                this.formatId = item.id;
                this.selectedPrice = item.price;
                this.selectedformat = item.title;
            },
            hideFormat() {
                this.popupVisible = false;
            }
        }
    })
    </script>
</body>

</html>